package Utils;

import com.alibaba.druid.pool.DruidDataSource;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class JdbcUtils {
    private Connection conn;
    private   void  getConnection(){
        DruidDataSource  druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/bill?serverTimezone=UTC");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");

        //配置连接池参数
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(20);
        druidDataSource.setMinIdle(5);
        try {
          conn =   druidDataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public   void  closeConnection(){
        try {
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public  Connection  getConn(){
        getConnection();
        return  conn;
    }
    public  Statement getStatement() throws SQLException {
        getConnection();
        Statement statement = conn.createStatement();
        return statement;
    }
    public PreparedStatement getPrepareStatement(String sql) throws SQLException {
        getConnection();
       return conn.prepareStatement(sql);
    }



}
